﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Pay.Core.http
{
   public  class HttpHelper
    {
        public static string HttpWebRequestAsync(string url,string token)
        {
            using (WebClient client = new WebClient())
            {
                client.Headers.Add("token", token);
                return Encoding.UTF8.GetString(client.DownloadData(url));
            }
        }

        public static bool SendAndGet(string sUrl, string SendData, ref string outputString)
        {
            try
            {
                //发送   
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sUrl);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                //webRequest.ContentType = "application/json";
                webRequest.Method = "post";
                webRequest.Timeout = 80000;
                UTF8Encoding encoding = new UTF8Encoding();//UTF8Encoding ASCIIEncoding
                byte[] data = encoding.GetBytes(SendData);

                webRequest.ContentLength = data.Length;
                Stream reqStream = webRequest.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
                //接收
                HttpWebResponse myResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
                {
                    outputString = sr.ReadToEnd();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static bool SendAndGet(string sUrl, string SendData, string contentType, ref string outputString)
        {
            try
            {
                //发送   
                HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(sUrl);
                webRequest.ContentType = contentType;// "application/x-www-form-urlencoded";
                webRequest.Method = "post";
                webRequest.Timeout = 80000;
                UTF8Encoding encoding = new UTF8Encoding();//UTF8Encoding ASCIIEncoding
                byte[] data = encoding.GetBytes(SendData);

                webRequest.ContentLength = data.Length;
                Stream reqStream = webRequest.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                reqStream.Close();
                //接收
                HttpWebResponse myResponse = (HttpWebResponse)webRequest.GetResponse();
                using (StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
                {
                    outputString = sr.ReadToEnd();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
        public static string HttpGet(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Referer = "http://ys.zbj.com";
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
    }
}
